package com.amikomgamedev.labirinhantu.state;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.controller.MultiTouch;
import org.andengine.input.touch.controller.MultiTouchController;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ClickDetector.IClickDetectorListener;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.opengl.view.RenderSurfaceView;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.AdMoob;
import com.amikomgamedev.labirinhantu.Cheat;
import com.amikomgamedev.labirinhantu.Memory_DB;
import com.amikomgamedev.labirinhantu.Utils;
import com.amikomgamedev.labirinhantu.entity.Entity_Camera_Menu;
import com.amikomgamedev.labirinhantu.game.Game_Data;
import com.amikomgamedev.labirinhantu.game.Game_Define;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Anim;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Data_Path;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Detach;
import com.amikomgamedev.labirinhantu.sound.Sound_Define;
import com.amikomgamedev.labirinhantu.sound.Sound_Manager;
import com.amikomgamedev.labirinhantu.text.Text_Manager;

public class State_Activity extends SimpleBaseGameActivity implements Game_Data, IUpdateHandler, IOnSceneTouchListener,
		IClickDetectorListener, IOnAreaTouchListener, IScrollDetectorListener, Game_Define
		, Grafik_Anim, Cheat, IOnScreenControlListener, IAnalogOnScreenControlListener, Grafik_Data_Path, Sound_Define
{
	protected static final byte				STATE_LOGO			= 0;
	protected static final byte				STATE_MENU_MAIN		= 1;
    protected static final byte                     	STATE_MENU_SELECT_STAGE         = 2;
    protected static final byte                             STATE_MENU_LEVEL                        = 3;
    protected static final byte                             STATE_MENU_CREDITS                      = 4;
    protected static final byte                             STATE_MENU_HELP                         = 5;
    protected static final byte                             STATE_GAME_PLAY                         = 6;
    protected static final byte                             STATE_GAME_PAUSE                        = 7;
    protected static final byte                             STATE_GAME_WIN                          = 8;
    protected static final byte                             STATE_GAME_OVER                         = 9;
	protected byte							State_Menu_Current	= -1;

	public static SimpleBaseGameActivity	activity;
	protected boolean						isCameraMove		= false;

	protected ZoomCamera					m_Camera;

	protected Scene							m_Scene				= new Scene();
	protected HUD							m_Hud				= new HUD();
	protected ClickDetector					m_ClickDetector		= new ClickDetector(this);
	protected Grafik_Detach					grafik				= new Grafik_Detach(this, m_Scene, m_Hud, m_Camera);
	protected Text_Manager					teks				= new Text_Manager(this, m_Scene, m_Hud, grafik);
	protected Entity_Camera_Menu			m_Camera_Menu;
	protected Sound_Manager					sound				= new Sound_Manager(this);
	protected ScrollDetector				m_Scroll 			= new ScrollDetector(this);

	protected Memory_DB		m_DB;
	// protected Entity_Camera_Menu m_Menu_Camera = new
	// Entity_Camera_Menu(m_Camera, grafik.spr_Menu_Main_Bg);
	
//	protected AdMoob m_Ads = new AdMoob(this);
	
	public EngineOptions onCreateEngineOptions()
	{
		activity = this;
		
		m_DB	= new Memory_DB(activity, "", null, 1);

		m_Camera = new ZoomCamera(0, 0, RES_WIDTH, RES_HEIGHT);
		m_Camera.setHUD(m_Hud);

		m_Camera_Menu = new Entity_Camera_Menu(m_Camera);

		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
				new RatioResolutionPolicy(RES_WIDTH, RES_HEIGHT), m_Camera);

		engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
		

//		    

		return engineOptions;
	}

	protected void onCreateResources()
	{
//	    	mRenderSurfaceView = new RenderSurfaceView(this);
//		m_Ads.createAds();
//		m_Ads.addAds(mRenderSurfaceView);
//		m_Ads.visibleAds();
	}

	protected Scene onCreateScene()
	{
//	    if (MultiTouch.isSupported(this))
//		    {
//			mEngine.setTouchController(new MultiTouchController());
//		    }
	    
		mEngine.registerUpdateHandler(new FPSLogger());
		mEngine.registerUpdateHandler(this);

		m_Scene.setOnSceneTouchListener(this);
		m_Scene.setOnAreaTouchListener(this);
		m_Hud.setOnAreaTouchListener(this);

		return m_Scene;
	}
	
	@Override
	protected void onSetContentView()
	{
//	    	mRenderSurfaceView = new RenderSurfaceView(this);
//		m_Ads.createAds();
//		m_Ads.addAds(mRenderSurfaceView);
//		m_Ads.visibleAds();
		
		super.onSetContentView();
	}

	public void onUpdate(float pSecondsElapsed)
	{
	}

	public void reset()
	{
	}

	protected void switchState(byte state)
	{
	}

	public void onClick(ClickDetector pClickDetector, int pPointerID, float pSceneX, float pSceneY)
	{
	}

	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		return false;
	}

	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		return false;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		return false;
	}

	public void onScrollStarted(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		// TODO Auto-generated method stub
		
	}

	public void onScroll(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		// TODO Auto-generated method stub
		
	}

	public void onScrollFinished(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		// TODO Auto-generated method stub
		
	}

	public void onControlChange(
		BaseOnScreenControl pBaseOnScreenControl,
		float pValueX,
		float pValueY)
	{
	    // TODO Auto-generated method stub
	    
	}

	public
		void
		onControlClick(AnalogOnScreenControl pAnalogOnScreenControl)
	{
	    // TODO Auto-generated method stub
	    
	}
	
	@Override
	public synchronized void onPauseGame()
	{
	    Utils.TRACE("ON RESUME");
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    Utils.TRACE("ON PAUSE");
	    super.onResumeGame();
	}
	
	@Override
	protected void onDestroy()
	{
//		m_Ads.destroyAds();

	super.onDestroy();
	}
}
